  using System;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Net;
  using System.Xml.Serialization;
  using System.IO;
  using System.Text;
  using System.Threading;
  using System.Collections;
  using System.Collections.Generic;
  using System.Timers;

  using MediaPortal.GUI.Library;
  using MediaPortal.Dialogs;
  using MediaPortal.Util;
  using MediaPortal.Localisation;
  using MediaPortal.Configuration;
  using MediaPortal.Player;
  using MediaPortal.Playlists;

  using YahooMusicEngine;
  using YahooMusicEngine.OnlineDataProvider;
  using YahooMusicEngine.Services;
  using YahooMusicEngine.Entities;
  using YahooMusicEngine.Locale;

  namespace YahooMusicPlugin
  {

    public class YahooMusicNowPlaying : YahooMusicGUIBase
    {
      #region skin connection
      [SkinControlAttribute(50)]
      protected GUIListControl catListControl = null;
      [SkinControlAttribute(51)]
      protected GUIListControl similarListControl = null;
      [SkinControlAttribute(13)]
      protected GUIVideoControl playerwindow = null;
      #endregion
      //public VideoResponse InfoItem;
      public override int GetID
      {
        get
        {
          return 27052;
        }

        set
        {
        }
      }

      public YahooMusicNowPlaying()
      {

      }

      public override bool Init()
      {
        provider.AppId = "DeUZup_IkY7d17O2DzAMPoyxmc55_hTasA--";
        provider.SharedSecret = "d80b9a5766788713e1fadd73e752c7eb";
        provider.Init();
        return Load(GUIGraphicsContext.Skin + @"\yahoomusicnowplaying.xml");
      }

      protected override void OnPageLoad()
      {
        _setting.Load();
        if (MessageGUI.NowPlaying != null && !string.IsNullOrEmpty(MessageGUI.NowPlaying.Video.Id))
        {
          GUIPropertyManager.SetProperty("#header.label", GUILocalizeStrings.Get(127083));
          Log.Debug("info for {0} {1}", MessageGUI.NowPlaying.Video.Title, MessageGUI.NowPlaying.Categories.Count.ToString());
          SimilarVideosService similar = new SimilarVideosService();
          similar.Id = MessageGUI.NowPlaying.Video.Id;
          provider.GetData(similar);
          //SetLabels(InfoItem, "Curent");
          GUIControl.ClearControl(GetID, catListControl.GetID);
          foreach (CategoryEntity cat in MessageGUI.NowPlaying.Categories)
          {
            GUIListItem item = new GUIListItem();
            // and add station name & bitrate
            item.Label = cat.Name;
            item.Label2 = "";
            item.IsFolder = true;
            item.MusicTag = cat;
            // MediaPortal.Util.Utils.SetDefaultIcons(item);
            catListControl.Add(item);
          }
          addVideos(similar.Items, similarListControl);
          playerwindow.SetVideoWindow = true;
          
        }
        base.OnPageLoad();
      }

      protected override void OnPageDestroy(int new_windowId)
      {
        base.OnPageDestroy(new_windowId);
      }

      void addVideos(List<VideoResponse> vids, GUIListControl listControl)
      {
        foreach (VideoResponse vi in vids)
        {
          if (filterVideoContens(vi))
          {
            GUIListItem item = new GUIListItem();
            // and add station name & bitrate
            item.Label = FormatTitle(vi);
            item.Label2 = "";
            item.IsFolder = false;
            item.ThumbnailImage = GetLocalImageFileName(vi.Image.Url);
            item.IconImage = "defaultVideoBig.png";
            item.IconImageBig = GetLocalImageFileName(vi.Image.Url);
            item.Year = vi.Video.CopyrightYear;
            item.Duration = vi.Video.Duration;
            item.Rating = vi.Video.Rating;
            item.OnItemSelected += new GUIListItem.ItemSelectedHandler(item_OnItemSelected);
            //DownloadImage(vi);
            item.MusicTag = vi;
            listControl.Add(item);
          }
        }
      }

      void item_OnItemSelected(GUIListItem item, GUIControl parent)
      {
        //      throw new Exception("The method or operation is not implemented.");
      }

      protected override void OnClicked(int controlId, GUIControl control, Action.ActionType actionType)
      {
        if (control == similarListControl)
        {
          VideoResponse item = similarListControl.SelectedListItem.MusicTag as VideoResponse;
          DoPlay(item);
        }
        else if (control == catListControl)
        {
          CategoryTreeService cat = new CategoryTreeService();
          CategoryEntity item = catListControl.SelectedListItem.MusicTag as CategoryEntity;
          cat.RootID = item.Id;
          cat.Type = item.Type;
          provider.GetData(cat);
          item.Childs = cat.Items;
          MessageGUI.Item = cat.Items[0];
          GUIWindowManager.ShowPreviousWindow();
        }

        base.OnClicked(controlId, control, actionType);
      }

    }
  }


